﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using CuongHanh.Context;
using CuongHanh.DAL;
using CuongHanh.Models;

namespace CuongHanh.Service
{
    public class OrderForGemService
    {
        public static bool Insert(OrderForGem model, List<OrderDetailForGem> orderDetailForGems,int customerId)
        {
            var serviceFee = CacheContext<ServiceFee>.Singleton.GetObject(model.ServiceFeeId);
            if(serviceFee!=null){
            long totalPrice = 0;
                using (UnitOfWork unitOfWork = new UnitOfWork())
                {
                    if(customerId > 0)
                        model.CustomerId = customerId;
                    model.OrderState = (int)OrderState.TaoMoi;
                    model.ShippingCost = serviceFee.ShippingCost;
                    model.UtilityFee = serviceFee.UtilityFee;
                    unitOfWork.Insert(model);
                    unitOfWork.Save();
                    foreach (var item in orderDetailForGems)
                    {
                        item.OrderForGemId = model.Id;
                        totalPrice += item.GetTotalPrice;
                        unitOfWork.Insert(item);
                    }
                    unitOfWork.Save();
                    model.Price = totalPrice;
                    model.OrderState = (int) OrderState.ChoThanhToan;
                    unitOfWork.Save();
                    return true;
                }
            }
            return false;
        }
    }
}